"""
Case Type   : 资源池化集群系统支持归档备份
Case Name   : 开启归档，设置wal_level=hot_standby,查看归档生成
Create At   : 2025/04/19
Owner       : @kyleze
Description :
    1.设置wal_level=hot_standby
    2.设置archive_dest
    3.设置archive_mode=on
    4.执行pg_switch_xlog
    5.查看归档文件是否存在
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.生成归档文件
History     :
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "dms_res" not in Common().get_sh_result(
        Node("PrimaryDbUser"),
        f"source {macro.DB_ENV_PATH};cm_ctl query -Cvidp"), "非资源池化不执行")
class SharedStorageAchiveXlog(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"----{os.path.basename(__file__)} start----")
        self.constant = Constant()
        self.com = Common()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.primary_node = Node("PrimaryDbUser")
        self.archive_dest = os.path.join(macro.DB_BACKUP_PATH,
                                         "archive_dir_013/")
        self.default_archive_mode = \
            self.com.show_param("archive_mode")
        self.default_archive_dest = \
            self.com.show_param("archive_dest")
        self.default_wal_level = \
            self.com.show_param("wal_level")

    def test_archive_xlog(self):
        step = "--step1:设置wal_level=minimal expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            f"wal_level=hot_standby")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)
        stop_res = self.primary_sh.exec_cm_ctl("stop")
        self.logger.info(stop_res)
        self.assertIn(self.constant.cm_stop_success_msg, stop_res,
                      "执行失败:" + step)
        start_res = self.primary_sh.exec_cm_ctl("start")
        self.logger.info(start_res)
        self.assertIn(self.constant.cm_start_success_msg, start_res,
                      "执行失败:" + step)

        step = "--step2:设置archive_mode=on expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode=on")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step3:设置archive_dest expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.archive_dest}'")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step4:执行pg_switch_xlog expect:成功--"
        self.logger.info(step)
        archive_res = self.primary_sh.execut_db_sql("select pg_switch_xlog();")
        self.logger.info(archive_res)
        self.assertIn("1 row", archive_res, "执行失败:" + step)

        step = "--step5:查看归档文件是否存在 expect:存在--"
        self.logger.info(step)
        cmd = f"ls {self.archive_dest}"
        self.logger.info(cmd)
        ls_archive = self.primary_node.sh(cmd).result()
        self.logger.info(ls_archive)
        archive_bool = 'No such file or directory' not in ls_archive and len(
            ls_archive.split()) > 0
        self.assertTrue(archive_bool, "执行失败:" + step)

    def tearDown(self):
        self.logger.info("----清理环境----")
        rm_cmd = f"rm -rf {self.archive_dest};"
        self.logger.info(rm_cmd)
        rm_msg = self.primary_node.sh(rm_cmd).result()
        self.logger.info(rm_msg)
        guc_res1 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode={self.default_archive_mode}")
        self.logger.info(guc_res1)
        guc_res2 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.default_archive_dest}'")
        self.logger.info(guc_res2)
        guc_res = self.primary_sh.execute_gsguc(
            "set", self.constant.GSGUC_SUCCESS_MSG,
            f"wal_level={self.default_wal_level}")
        self.logger.info(guc_res)
        stop_res = self.primary_sh.exec_cm_ctl("stop")
        self.logger.info(stop_res)
        start_res = self.primary_sh.exec_cm_ctl("start")
        self.logger.info(start_res)
        self.assertTrue(guc_res, "参数设置失败")
        self.assertIn(self.constant.cm_stop_success_msg, stop_res,
                      "停止集群失败")
        self.assertIn(self.constant.cm_start_success_msg, start_res,
                      "启动集群失败:")
        self.assertTrue(guc_res1, "设置参数失败")
        self.assertTrue(guc_res2, "设置参数失败")
        self.assertEqual("", rm_msg, "清理目录失败")
        self.logger.info(f"----{os.path.basename(__file__)} end----")
